﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;


public partial class admin_leftMenu : System.Web.UI.Page
{
      readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly DALRoleRuleRef theRoleRuleRef = new DALRoleRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();

        public StringBuilder SbTreeHtml = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.ValidateLoginRule();  
            }
        }

          void ValidateLoginRule()
        {

            SbTreeHtml.Append("<script language=\"javascript\" type=\"text/jscript\"> \n");
            SbTreeHtml.Append("ImgDir = \"leftimage/\";\n");
            SbTreeHtml.Append("SetFolderIcon(\"desktop.gif\");");
            SbTreeHtml.Append("treeMenuAddItem(0, \"&nbsp系统功能操作菜单\");\n");
            SbTreeHtml.Append("SetFolderIcon();\n");
            SbTreeHtml.Append("var MenuColor=top.MenuColor;\n");
            SbTreeHtml.Append("var MenuTextColor=\"#000000\";\n");

            int iAdminId = int.Parse(new BaseAdmin().AdminUserId);
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);
            if (oCompany != null)
            {
                if (oCompany.IsDelete)
                {
                    base.Response.Write("<script language=javascript>alert('贵公司账号已被禁用，如使用请与客服联系！');window.top.location.href='login.aspx';</script>");
                    base.Response.End();
                }
                BaseAdmin theBaseAdmin = new BaseAdmin();
                theBaseAdmin.CompanyId = oCompany.Id.ToString();
                theBaseAdmin.LevelId = oCompany.LevelId.ToString();

                DataSet dsMain = theRoleRuleRef.GetMainListByRoleIdForCompany(iAdminId);

                DataSet ds0 = theRoleRuleRef.GetListByRoleIdForCompany(iAdminId);
                if (dsMain != null && dsMain.Tables.Count > 0 && ds0 != null && ds0.Tables.Count > 0)
                {

                    foreach (DataRow drMain in dsMain.Tables[0].Rows)
                    {
                        SbTreeHtml.Append("treeMenuAddItem(1, \"" + drMain["CompanyRule"] + "\");\n");
                        foreach (DataRow dr in ds0.Tables[0].Rows)
                        {
                            if (drMain["Id"].ToString() == dr["ParentId"].ToString())
                            {
                                string sTempUrl = string.Empty;
                                if (dr["Url"].ToString().Contains("?moduid"))
                                {
                                    sTempUrl = dr["Url"].ToString();
                                }
                                else
                                {
                                    sTempUrl = dr["Url"].ToString().Contains("?") ? dr["Url"].ToString().Trim() + "&moduid=" + dr["RuleId"] : dr["Url"].ToString().Trim() + "?moduid=" + dr["RuleId"];
                                }
                                SbTreeHtml.Append("treeMenuAddItem(2, \"" + dr["CompanyRule"] + "\", \"" + sTempUrl+ "\", \"tabWin\",\"l4.gif\");\n");
                            }
                        }
                    } 
                }
            }

            DataSet dsRoleList = theRoleRuleRef.GetRoleListByAdminId(iAdminId);
              if(dsRoleList != null && dsRoleList.Tables.Count > 0)
                  foreach (DataRow drRole in dsRoleList.Tables[0].Rows)
                  {
                      int iRoleId = int.Parse(drRole["id"].ToString());
                      DataSet dsSub = theRoleRuleRef.GetMainListByRoleId(iRoleId);
                      DataSet ds1 = theRoleRuleRef.GetListByRoleId(iRoleId);
                      if (dsSub != null && dsSub.Tables.Count > 0 && ds1 != null && ds1.Tables.Count > 0)
                      {
                          foreach (DataRow drSub in dsSub.Tables[0].Rows)
                          {
                              SbTreeHtml.Append("treeMenuAddItem(1, \"" + drSub["Description"] + "\");\n");
                              foreach (DataRow dr in ds1.Tables[0].Rows)
                              {

                                  if (drSub["RuleId"].ToString() == dr["ParentId"].ToString())
                                  {
                                      if (dr["Url"] != null && dr["Url"].ToString() != string.Empty)
                                      {
                                          string sTempUrl = string.Empty;
                                          if (dr["Url"].ToString().Contains("?moduid"))
                                          {
                                              sTempUrl = dr["Url"].ToString();
                                          }
                                          else
                                          {
                                              sTempUrl = dr["Url"].ToString().Contains("?") ? dr["Url"].ToString().Trim() + "&moduid=" + dr["RuleId"] : dr["Url"].ToString().Trim() + "?moduid=" + dr["RuleId"];
                                          }
                                          SbTreeHtml.Append("treeMenuAddItem(2, \"" + dr["Description"] + "\", \"" + sTempUrl + "\", \"tabWin\",\"l4.gif\");\n");
                                      }
                                  }
                              }
                          }
                      }
                  }

             SbTreeHtml.Append("treeMenuAddItem(1, \"个人设置\");\n");
            SbTreeHtml.Append("treeMenuAddItem(2, \"修改密码\", \"System/EditUserPas.aspx\", \"tabWin\",\"l4.gif\");\n");
            SbTreeHtml.Append("</script>\n");
            SbTreeHtml.Append("<script language=\"JavaScript\" type=\"text/jscript\">\n");
            SbTreeHtml.Append("initializeDocument();\n");
            SbTreeHtml.Append("clickOnNode(0);\n");
            SbTreeHtml.Append("</script>\n");
        }
    }

